package com.credithc.fastdfs.monitor.web;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.csource.common.MyException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.credithc.fastdfs.api.entity.Alarm;
import com.credithc.fastdfs.api.entity.SystemArea;
import com.credithc.fastdfs.monitor.service.AlarmService;
import com.credithc.framework.core.mybatis.page.PageController;
import com.credithc.framework.core.mybatis.page.PageDto;
import com.credithc.framework.core.utils.DataGrid;
import com.credithc.framework.core.utils.JsonResult;

/** 
 * @ClassName: AlarmController 
 * @Description:  告警管理
 * @author zhaoc
 * @date 2015年6月8日
 * @time  上午10:07:27 
 */ 
@Controller
@RequestMapping(value = "/alarm")
public class AlarmController {

	private static final Logger log = LoggerFactory
			.getLogger(AlarmController.class);

	@Resource(name = "alarmServiceImpl")
	public AlarmService alarmServiceImpl;

	@RequestMapping(value = "alert")
	public ModelAndView fileManage() {
		ModelAndView mv = new ModelAndView("alarm/alert");
		return mv;
	}

	@ResponseBody
	@RequestMapping(value = "alertToJson")
	public DataGrid listFileManage(PageDto pageDto, Alarm info,
			@RequestParam("sort") String sort,
			@RequestParam("order") String order, HttpServletRequest request, 
			HttpServletResponse response) {
		PageController pc = pageDto.createPage();
		pc.setSort(sort);
		pc.setOrder(order);
		return DataGrid.formateList(alarmServiceImpl.selectAlarmList(pc, info));
	}
	
	/** 
	 * @Title: addAlarm 
	 * @Description:新增告警提醒 
	 * @param 
	 * @return JsonResult
	 * @throws 
	 */ 
	@ResponseBody
	@RequestMapping(value = "addAlarm", method = RequestMethod.POST)
	public JsonResult addAlarm(Alarm alarm, HttpServletRequest request) throws MyException {
		int o = alarmServiceImpl.addAlarm(alarm);
		log.info("添加告警 : " + o);
		JsonResult jr = new JsonResult();
		jr.setSuccess(true);
		jr.setMsg("告警提醒添加成功!");
		return jr;
	}
	
	/** 
	 * @Title: initEditAlarm 
	 * @Description: 初始化修改页面
	 * @param 
	 * @return JsonResult
	 * @throws 
	 */ 
	@ResponseBody
	@RequestMapping(value = "initEditAlarm", method = RequestMethod.POST)
	public JsonResult initEditAlarm(@RequestParam("alarmId") String alarmId, HttpServletRequest request) throws MyException {
		Alarm alarm = alarmServiceImpl.getAlarmBySysId(alarmId);
		log.info("初始化编辑告警信息 : " + alarm);
		JsonResult jr = new JsonResult();
		if(alarm != null) {
			jr.setSuccess(true);
			jr.setObj(alarm);
		} else {
			jr.setSuccess(false);
			jr.setMsg("告警信息未找到");
		}
		return jr;
	}
	
	/** 
	 * @Title: editSysParam 
	 * @Description: 修改告警信息
	 * @param 
	 * @return JsonResult
	 * @throws 
	 */ 
	@ResponseBody
	@RequestMapping(value = "editAlarm", method = RequestMethod.POST)
	public JsonResult editSysParam(Alarm alarm, HttpServletRequest request) throws MyException {
		int o = alarmServiceImpl.editAlarm(alarm);
		log.info("告警信息参数 : " + o);
		JsonResult jr = new JsonResult();
		jr.setSuccess(true);
		jr.setMsg("告警信息修改成功!");
		return jr;
	}
	
	/** 
	 * @Title: delAlarm 
	 * @Description: 删除告警信息
	 * @param 
	 * @return JsonResult
	 * @throws 
	 */ 
	@ResponseBody
	@RequestMapping(value = "delAlarm", method = RequestMethod.POST)
	public JsonResult delAlarm(@RequestParam("alarmIds") List<String> alarmIds, HttpServletRequest request) throws MyException {
		int o = alarmServiceImpl.delAlarm(alarmIds);
		JsonResult jr = new JsonResult();
		jr.setSuccess(true);
		jr.setMsg("告警信息删除成功!");
		return jr;
	}
}
